/*
 * File:   main.cpp
 * Author: Chris
 *
 * Created on March 4, 2011, 2:29 AM
 */



#ifdef _CILK_
#include "cilk.h"
#endif

#include <stdlib.h>
#include <iostream>
#include <string>
#include <time.h>
#include "Game.h"
#include "Board.h"
#include "PChess.h"
// Thank you for this file :)
#include "example_util_gettime.h"
#include <string.h>

using namespace std;

// Prototypes
void newGame();
void readGame();
void playGame(Game);
void processArgs(int, char**);
bool validInput(int, char);

// Global vars
bool alphabeta = false;

int main(int argc, char** argv)
{
    processArgs(argc, argv);
    string op;
    
    while(true)
    {
        cout << "Enter 'play' to start a new game, 'read' to input a position, or 'quit' to close the game." << endl;
    
        cin >> op;
        if(op == "play")
        {
            newGame();
        }
        else if(op == "read")
        {
            readGame();
        }
        else if(op == "quit")
        {
            break;
        }
    }

    return (EXIT_SUCCESS);
}

void newGame()
{
    Game chess("", alphabeta);

    cout << "New game created! " << endl;
    cout << "You are playing as BLACK" << endl << endl;
    
    playGame(chess);
}

void readGame()
{
    string boardLayout = "";

    cout << "Reading user board layout\n";
    for (int i = 0; i < 8; i++)
    {
        string next;
        cin >> next;
        //boardLayout.append("\n" + next);
        boardLayout += next + "\n";
    }
    
    Game chess(boardLayout, alphabeta);
    playGame(chess);
}

void playGame(Game chess)
{
    char from1, to1;
    int from2, to2;
    int status;
    long time1, time2;

    // Print the initial game board
    chess.printGame();

    status = chess.startGame(WHITE);
    // Print the update game board after the first move
    chess.printGame();

    while(status == IN_PROGRESS)
    {
        cout << "Input move (e.g. 'b2 b4')" << endl;
        cin >> from1 >> from2;
        if(cin.fail() || !validInput(from2, from1))
            {
                cin.ignore(1000,'\n');
                cin.clear();
                continue;
            }



        if(from1 == 'x')
        {
                time1 = example_get_time();
        	cout << "Castling...\n";
        	chess.castle(false, from2);
                cin.clear();
        }
        else
        {
            cin >> to1 >> to2;
            if(cin.fail() || !validInput(from2, from1))
            {
                cin.ignore(1000,'\n');
                cin.clear();
                continue;
            }
            
            time1 = example_get_time();
            status = chess.humanMove((from2-1),(from1-'a'),(to2-1),(to1-'a'));
            cin.clear();
        }
        time2 = example_get_time();
        cout << "Time Elapsed: " << ((time2-time1)/1000.0) << "s" << endl << endl;

        chess.printGame();
    }
}

void processArgs(int argc, char* argv[])
{
    if(argc > 1)
    {
        if(strcmp(argv[1],"-ab")==0)
        {
            alphabeta = true;
            cout << "Using alpha-beta pruning"<< endl;
        }
    }
}

bool validInput(int row, char col)
{
    if(row < 1 || row > 8)
        return false;

    if(col < 'a' || col > 'h')
        return false;

    return true;
}